package com.travelsky.airchina.boarding.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.travelsky.airchina.boarding.dao.RoleDAO;
import com.travelsky.airchina.boarding.domain.Role;
import com.travelsky.airchina.boarding.domain.RoleColumn;
import com.travelsky.airchina.boarding.service.RoleService;


@Service("roleService")
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleDAO roleDAO;
	public List<Role> findAllRoles() {
		return roleDAO.findAll();
	}

	public List<Role> findAllRoles(String status) {
		return roleDAO.findAllRoles(status);
	}

	public Role getRole(Long id) {
		return roleDAO.findById(id);
	}

	public void saveOrUpdateRole(Role role) {
		boolean isCreateRole = false;
		if(role.getId()==null){
			isCreateRole = true;
		}
		roleDAO.makePersistent(role);
		if(!isCreateRole){
			roleDAO.deleteRoleColumnByRoleId(role.getId());
		}
		if(role.getRoleColumns()!=null && role.getRoleColumns().size()>0){
			for(RoleColumn temp:role.getRoleColumns()){
				temp.setRoleId(role.getId());
				roleDAO.saveRoleColumn(temp);
			}
		}
	}

	public int getRoleSum() {
		return roleDAO.getRoleSum();
	}

	public Role getRoleWithColumn(Long id) {
		Role role = roleDAO.findById(id);
		List<RoleColumn> list = roleDAO.findColumnByRoleId(id);
		role.setRoleColumns(list);
		return role;
	}

	public List<RoleColumn> findColumnByRoleId(Long roleId) {
		return roleDAO.findColumnByRoleId(roleId);
	}

}
